<template>
  <DetailsFormBox :title="dataInfo?.table?.title" :type="route.query.type" :form="data.form" :btns="[
    {
      title: '重置',
      icon: '',
      type: '',
      click: getForm,
    },
    {
      title: '保存',
      icon: '',
      type: 'primary',
      click: (form: any) => {
        saveBtn(form)
      },
    },
  ]" :formInfo="[
    {
      label: '创建人员',
      value: 'creatorName',
    },
    {
      label: '创建时间',
      value: 'createTime',
    },
  ]" :loading="data.loading" :json="dataInfo">
    <template #default="{ form }">
      <div class="formMain">
        <ElFormItemBox label="刀模名称" :prop="`knifeVersionName`" isRules>
          <div class="flex flexAuto list-r-8">
            <ElInputBox :data="form" dataName="knifeVersionName" />
            <SelectProduct @selectBtn="selectBtn">
              <el-button>选择产品</el-button>
            </SelectProduct>
          </div>
        </ElFormItemBox>
        <ElFormItemBox label="刀模号" :prop="`knifeVersionNumber`">
          <ElInputBox :data="form" dataName="knifeVersionNumber" />
        </ElFormItemBox>
        <ElFormItemBox label="制作日期" :prop="`useDate`">
          <ElDayBox :data="form" dataName="useDate" />
        </ElFormItemBox>
        <ElFormItemBox label="存放位置" :prop="`storageLocation`">
          <ElInputBox :data="form" dataName="storageLocation" />
        </ElFormItemBox>
        <ElFormItemBox label="刀模尺寸" :prop="`knifeVersionSize`">
          <ElInputBox :data="form" dataName="knifeVersionSize" unit="mm" />
        </ElFormItemBox>
        <ElFormItemBox label="小标" :prop="`smallLabel`">
          <ElInputBox :data="form" dataName="smallLabel" unit="mm" />
        </ElFormItemBox>
        <ElFormItemBox label="中标" :prop="`mediumLabel`">
          <ElInputBox :data="form" dataName="mediumLabel" unit="mm" />
        </ElFormItemBox>
        <ElFormItemBox label="大标" :prop="`grandLabel`">
          <ElInputBox :data="form" dataName="grandLabel" unit="mm" />
        </ElFormItemBox>
        <ElFormItemBox label="模数" :prop="`modulus`">
          <ElInputBox :data="form" dataName="modulus" />
        </ElFormItemBox>
        <ElFormItemBox label="使用刀次" :prop="`usedKnifeTimes`">
          <ElInputBox :data="form" dataName="usedKnifeTimes" unit="次" disabled />
        </ElFormItemBox>
        <ElFormItemBox label="备注" :prop="`knifeVersionRemark`">
          <ElInputBox :data="form" dataName="knifeVersionRemark" type="textarea" />
        </ElFormItemBox>
      </div>
    </template>
  </DetailsFormBox>
</template>
<script setup lang="ts">
import { useListComponent } from '../pageTs'; // 引入逻辑
const { dataInfo, options } = useListComponent();
import { reactive, onMounted, getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance() as any;
const data: any = reactive({
  loading: false,
  loadingMsg: '',
  form: {
    knifeVersionState: 0, // 刀版状态：0制作中；1已完成；2已作废
    knifeVersionSize: "", // 刀版尺寸
    knifeVersionType: 1, // 刀版类型：0私用，1公用
    customerCode: "", // 客户编号
    modulus: "", // 刀版模数
    jumpDistance: null, // 跳距
    usedKnifeTimes: 0, // 已使用刀次
    warningKnifeTimes: 0, // 预警刀次
    useDate: "", // 使用日期（date-time 格式）
    serviceLife: 0, // 使用期限（月）
    knifeVersionRemark: "", // 刀版备注
    isCopy: false, // 是否复制创建
    knifeVersionName: "", // 刀版名称
    storageLocation: "", // 存放位置
    knifeVersionNumber: "", // 刀版号
    smallLabel: "", // 小标
    mediumLabel: "", // 中标
    grandLabel: "", // 大标

    // 刀版明细数据数组
    detailList: [
      // {
      //   knifeDetailID: 0, // 刀版明细主键ID
      //   knifeDetailNo: "", // 刀版明细编号
      //   knifeVersionCode: "", // 刀版编号
      //   width: 0.0, // 宽
      //   height: 0.0, // 高
      //   shape: 0, // 形状
      //   rAngle: 0.0, // R角
      //   spacing: 0.0, // 间距
      //   direction: 0, // 方向
      //   quantity: 0, // 数量
      //   remark: "" // 备注
      // }
    ],

    // 刀版文件数据数组
    fileList: [
      // {
      //   knifeVersionFileID: 0, // 刀版文件主键ID
      //   knifeVersionFileNo: "", // 刀版文件编号
      //   knifeVersionCode: "", // 刀版编号
      //   knifeVersionFileName: "", // 文件名称
      //   knifeVersionFileSize: 0.0, // 文件大小（单位 KB）
      //   knifeVersionFileUrl: "", // 文件路径
      //   knifeVersionFilePreview: "", // 预览图地址
      //   uploadPerson: "", // 上传人员
      //   uploadTime: "" // 上传时间（date-time 格式）
      // }
    ]
  },
});
import { useRoute } from 'vue-router';
// 获取当前路由对象
const route = useRoute();
import { sendMsg } from '@/utils/crossTagMag.ts';
//选择产品
const selectBtn = (arr: any) => {
  const _row = arr[0] || {}
  const _form = data.form
  _form.knifeVersionName = _row.productName
  proxy.$api.OrderBusniess.GetSingleProductByProductCode({
    queryBy: 2,
    queryValue: _row.productCode,
  })
    .then((res: any) => {
      _form.knifeVersionName = res.baseData.productName
      _form.knifeVersionSize = res.baseData.productionDimensions
      _form.modulus = res.baseData.makeupModules
      const _labelArr = ['grandLabel', 'mediumLabel', 'smallLabel']
      _labelArr.forEach((el: any, index: number) => {
        console.log(el);
        const _labelRow = res.productTagList[index]
        if (_labelRow) {
          _form[el] = `${_labelRow.tagWidth}*${_labelRow.tagHeight}`
        } else {
          _form[el] = ""
        }
      });

    })
    .catch(() => {

    });
}
const saveBtn = (form: any) => {
  if (form.knifeVersionCode) {
    proxy.$api.ProductionBusniess.UpdateKnifeVersion(form)
      .then((res: any) => {
        _sun()
      })
      .catch(() => { });
  } else {
    proxy.$api.ProductionBusniess.CreateKnifeVersion(form)
      .then((res: any) => {
        _sun()
      })
      .catch(() => { });
  }

  function _sun() {
    proxy.$message('保存成功')
    sendMsg('save', true);
    // getForm()
    window.close();
  }
};
const getForm = () => {
  const _query = route.query
  if (_query.knifeVersionID) {
    proxy.$api.ProductionBusniess.GetSingleKnifeVersionView({
      queryBy: 1,
      queryValue: route.query.knifeVersionID
    })
      .then((res: any) => {
        data.form = res
      })
      .catch(() => { });
  }
};
onMounted(() => {
  getForm()
});
</script>
